package com.szholly.pro.watergas_server.common.data;

import com.szholly.data.general.RowBase;
import com.szholly.plug.form.excel.utils.StringUtil;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class DataHelper {

	/**
	 * 行转列
	 * @param data 
	 * @param combiteField 依据哪个字段合并行
	 * @param rowToColFields 哪些字段要行转列
	 * @param colTextField 哪一列要由行放到列，比如污染物
	 */
	public static List<RowBase> RowToCol(List<RowBase> data, String combiteField, List<String> rowToColFields, String colTextField){
		List<RowBase> result = new LinkedList<>();
		
		String curCombiteValue = null;
		RowBase curRow = new RowBase();
		for(RowBase row : data){
			String colText = row.getSafeString(colTextField);
			String combiteValue = row.getSafeString(combiteField);
			if(!combiteValue.equals(curCombiteValue)){
				curRow = new RowBase();
				if(curRow != null){
					result.add(curRow);
				}				
			}
			
			String[] columns = row.getColumns();
			
			for(String column:columns){
				if(rowToColFields.contains(column)){
					curRow.setValue(column+"_"+colText, row.get(column));
				}
				else{
					curRow.setValue(column, row.get(column));
				}				
			}
			
			curCombiteValue = combiteValue;
		}
		return result;		
	}
	
	/**
	 * 单元格合并
	 * @return
	 */
	public static List<RowBase> RowMergeEX(List<RowBase> data,List<String> mergeFields){
		
		RowBase preRow = null;
		
		for(RowBase row : data){
			
			if(preRow == null){
				preRow = (RowBase) row.clone();
				continue;
			}
			
			for(String fieldName : mergeFields){
				String preValue = preRow.getSafeString(fieldName);
				String curValue = row.getSafeString(fieldName);
				
				if(!StringUtil.IsNotNullOrEmpty(curValue)){
					continue;
				}
				
				if(curValue.equals(preValue)){
					row.setValue(fieldName, null);
					row.setValue(fieldName+"_has_parent", true);
				}
				else{
					preRow = (RowBase) row.clone();
				}
			}
		}
		return data;
	}
	
	/**
	 * 单元格合并
	 * @return
	 */
	public static List<RowBase> RowMerge(List<RowBase> data,List<String> mergeFields){
		
		RowBase preRow = null;
		HashMap<String, Integer> rowSpanMap = new HashMap<>();
		
		for(RowBase row : data){
			String fieldName = mergeFields.get(0);
			
			if(rowSpanMap.get(fieldName+"_row_span") == null){
				rowSpanMap.put(fieldName+"_row_span", 1);
			}
			
			if(preRow == null){
				preRow = (RowBase) row.clone();
				row.setValue(fieldName+"_row_span", rowSpanMap);
				continue;
			}
			
			String preValue = preRow.getSafeString(fieldName);
			String curValue = row.getSafeString(fieldName);
			
			if(!StringUtil.IsNotNullOrEmpty(curValue)){
				continue;
			}
			
			if(curValue.equals(preValue)){
				int rowSpan = rowSpanMap.get(fieldName+"_row_span");
				rowSpan++;
				rowSpanMap.put(fieldName+"_row_span", rowSpan);
				row.setValue(fieldName, null);
				row.setValue(fieldName+"_has_parent", true);
				row.setValue(fieldName+"_row_span", rowSpanMap);
			}
			else{
				rowSpanMap = new HashMap<>();
				row.setValue(fieldName+"_row_span", rowSpanMap);
				preRow = (RowBase) row.clone();
			}
		}
		return data;
	}

}
